# MODULES AND SETUP

# load completion
autoload -Uz compinit
compinit

# set prompt
autoload -Uz colors && colors
PROMPT=" %{${fg_bold[red]}%}%3~%(0?. . %{${fg[red]}%}%? )%{${fg[blue]}%}»%{${reset_color}%} "

# colorize completion
zstyle ':completion:*' list-colors ''

# case insensitive completion
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

# set history file and set size to 25
HISTFILE="$HOME/.zhistory"
HISTSIZE=25
SAVEHIST=25

# history search
autoload -Uz up-line-or-beginning-search down-line-or-beginning-search
zle -N up-line-or-beginning-search
zle -N down-line-or-beginning-search

# bind keys
bindkey "${terminfo[khome]}" beginning-of-line	# Home
bindkey "${terminfo[kend]}" end-of-line			# End
bindkey "${terminfo[kdch1]}" delete-char
bindkey '^[[1;5C' forward-word					# Ctrl + Right
bindkey '^[[1;5D' backward-word					# Ctrl + Left

# let patterns not matching a filename kept unchanged
setopt NO_NOMATCH

# GENERAL ALIASES

# modified commands
alias diff='colordiff'
alias grep='grep --color=auto --ignore-case'
#alias df='df -h'
#alias du='du -c -h'
alias mkdir='mkdir -p -v'
#alias dmesg='dmesg -HL'

# ls
#alias ls='ls -hF --color=auto'
alias ls='ls -h --color=auto'

# safety features
alias cp='cp -i'
alias mv='mv -i'
alias ln='ln -i'
alias chown='chown --preserve-root'
alias chmod='chmod --preserve-root'
alias chgrp='chgrp --preserve-root'

# error friendly cd
alias cd..='cd ..'

# pacman aliases
alias pacupg='sudo pacman -Syu'        # Synchronize with repositories before upgrading packages that are out of date on the local system.
alias pacin='sudo pacman -S'           # Install specific package(s) from the repositories
alias pacins='sudo pacman -U'          # Install specific package not from the repositories but from a file
alias pacre='sudo pacman -R'           # Remove the specified package(s), retaining its configuration(s) and required dependencies
alias pacrem='sudo pacman -Rns'        # Remove the specified package(s), its configuration(s) and unneeded dependencies
alias pacrep='pacman -Si'              # Display information about a given package in the repositories
alias pacreps='pacman -Ss'             # Search for package(s) in the repositories
alias pacloc='pacman -Qi'              # Display information about a given package in the local database
alias paclocs='pacman -Qs'             # Search for package(s) in the local database

# recursively remove all orphaned packages
alias pacro="/usr/bin/pacman -Qtdq > /dev/null && sudo /usr/bin/pacman -Rns \$(/usr/bin/pacman -Qtdq | sed -e ':a;N;\$!ba;s/\n/ /g')"

# get pacman log
alias paclog="$PAGER /var/log/pacman.log"

# pacaur aliases
alias pain='pacaur -Sa'
alias paupg='pacaur -Syu'

# for dotfiles
alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME"

# set wallpaper
alias cw='feh --randomize --bg-fill /home/mittens/Pictures/Wallpapers/Current/*'

# stream with youtube-dl
alias ytdl-stream='youtube-dl $1 -o - | mpv -'

# recompile dwm/st/surf
alias redwm="cd $HOME/Documents/workspace/dwm; updpkgsums; makepkg -fi --noconfirm"
alias rest="cd $HOME/Documents/workspace/st; updpkgsums; makepkg -fi --noconfirm"
alias resurf="cd $HOME/Documents/workspace/surf; updpkgsums; makepkg -fi --noconfirm"

# spt pause/unpause and time
alias pstart="nohup spt -e $HOME/.bin/ping.sh &>/dev/null &"
alias ptime="pkill --signal 10 -x 'spt'"
alias ppause="pkill --signal 12 -x 'spt'"

# MISCELLANEOUS

# add ~/.bin to $PATH
export PATH=$PATH:$HOME/.bin

# disable start and stop mappings (rtorrent)
stty stop undef
stty start undef

# use Ctrl + O to run prepend sudo to current line
prepend-sudo()
{
    [[ -z $BUFFER ]] && zle up-history
    [[ $BUFFER != sudo\ * ]] && LBUFFER="sudo $LBUFFER"
}
zle -N prepend-sudo
bindkey '^O' prepend-sudo
